package org.jeecg.modules.basic.plan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.basic.exitstock.entity.LwgyMaterialExitstock;
import org.jeecg.modules.basic.exitstock.mapper.LwgyMaterialExitstockMapper;
import org.jeecg.modules.basic.exitstock.service.ILwgyMaterialExitstockService;
import org.jeecg.modules.basic.plan.entity.LwgyMaterialPlanGoods;
import org.jeecg.modules.basic.plan.entity.LwgyMaterialPlanapplication;
import org.jeecg.modules.basic.plan.entity.LwgyMaterialPlanapplications;
import org.jeecg.modules.basic.plan.mapper.LwgyMaterialPlanGoodsMapper;
import org.jeecg.modules.basic.plan.mapper.LwgyMaterialPlanapplicationMapper;
import org.jeecg.modules.basic.plan.service.ILwgyMaterialPlanapplicationService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 物料管理申请表
 * @Author: jeecg-boot
 * @Date:   2021-04-06
 * @Version: V1.0
 */
@Service
public class LwgyMaterialPlanapplicationServiceImpl extends ServiceImpl<LwgyMaterialPlanapplicationMapper, LwgyMaterialPlanapplication> implements ILwgyMaterialPlanapplicationService {
    @Resource
    private LwgyMaterialPlanapplicationMapper lwgyMaterialPlanapplicationMapper;

    @Resource
    private LwgyMaterialPlanGoodsMapper lwgyMaterialPlanGoodsMapper;

    @Override
    public void saveMain(LwgyMaterialPlanapplication lwgyMaterialPlanapplication, List<LwgyMaterialPlanGoods> lwgyMaterialPlanGoodsList) {
        lwgyMaterialPlanapplicationMapper.insert(lwgyMaterialPlanapplication);
        if(lwgyMaterialPlanGoodsList!=null && lwgyMaterialPlanGoodsList.size()>0) {
            for(LwgyMaterialPlanGoods entity:lwgyMaterialPlanGoodsList) {
                //外键设置
                entity.setPlanId(lwgyMaterialPlanapplication.getId());
                lwgyMaterialPlanGoodsMapper.insert(entity);
            }
        }
    }

    @Override
    public void updateMain(LwgyMaterialPlanapplication lwgyMaterialPlanapplication, List<LwgyMaterialPlanGoods> lwgyMaterialPlanGoodsList) {
        lwgyMaterialPlanapplicationMapper.updateById(lwgyMaterialPlanapplication);

        //1.先删除子表数据
         lwgyMaterialPlanGoodsMapper.deleteByMainId(lwgyMaterialPlanapplication.getId());

       //2.子表数据重新插入
        if(lwgyMaterialPlanGoodsList!=null && lwgyMaterialPlanGoodsList.size()>0) {
            for(LwgyMaterialPlanGoods entity:lwgyMaterialPlanGoodsList) {
                //外键设置
                entity.setPlanId(lwgyMaterialPlanapplication.getId());
                lwgyMaterialPlanGoodsMapper.insert(entity);
            }
        }
    }

    @Override
    public void delMain(String id) {
        lwgyMaterialPlanGoodsMapper.deleteByMainId(id);
        lwgyMaterialPlanapplicationMapper.deleteById(id);
    }

    @Override
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for(Serializable id:idList) {
            lwgyMaterialPlanGoodsMapper.deleteByMainId(id.toString());
            lwgyMaterialPlanapplicationMapper.deleteById(id);
        }
    }

    @Override
    public IPage<LwgyMaterialPlanapplication> queryPageCustomer(Page<LwgyMaterialPlanapplication> page, QueryWrapper<LwgyMaterialPlanapplication> queryWrapper,String itemName,String category) {
       /*queryWrapper.groupBy("mp.id");*/
       /* queryWrapper.orderByDesc("writerTime")*/
        return page.setRecords(lwgyMaterialPlanapplicationMapper.queryPageListCustom(page,queryWrapper,itemName,category));
    }

    @Override
    public List<LwgyMaterialPlanapplications> queryPageExportListCustom(LwgyMaterialPlanapplications lwgyMaterialPlanapplications) {
        return lwgyMaterialPlanapplicationMapper.queryPageExportListCustom(lwgyMaterialPlanapplications);
    }

}
